package com.jsmtr.rms.controller.SystemLog;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.SystemLog.SystemLogService;
import com.jsmtr.rms.utils.ResultBean;
import com.jsmtr.rms.utils.StringTools;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: cyx
 * @description:
 * @date: Created in 2019/4/14 14:51
 * @modified by:
 */
@Controller
public class SystemLogController {
    @Autowired
    private SystemLogService systemLogService;

    @ResponseBody
    @RequestMapping(value = "/SystemLog/UserLog")
    public ResultBean getUserLog(HttpServletRequest request, HttpServletResponse response){
        ResultBean resultBean=new ResultBean();
        int perpage=1;
        int page=500;
        String sperpage=request.getParameter("page");
        String spage=request.getParameter("limit");
        if (StringTools.trimNotEmpty(sperpage)){
            perpage=Integer.parseInt(sperpage);
        }
        if(StringTools.trimNotEmpty(spage)){
            page=Integer.parseInt(spage);
        }
        String cardNo=request.getParameter("cardno");
        String proid=request.getParameter("proid");
        String userid=request.getParameter("userid");
        try {
            if (StringTools.trimNotEmpty(cardNo)) {
                Map params=new HashMap();
                if (StringTools.trimNotEmpty(cardNo)){
                    params.put("cardno",cardNo);
                }
                if(StringTools.trimNotEmpty(proid)){
                    params.put("proid",proid);
                }
                if(StringTools.trimNotEmpty(userid)){
                    params.put("userid",userid);
                }
               List<Map> logs= systemLogService.getDUserResumeLog(perpage,page, params);
                List<SystemLog> logList=new ArrayList<>();
                    if(logs.size()>0){
                        for (Map info : logs) {
                            SystemLog log=new SystemLog();
                            String flowname=info.get("FNAME")==null?info.get("NICKNAME").toString():info.get("FNAME").toString();

                            log.setLogid(info.get("LOGID").toString());
                            log.setProid(info.get("PROID").toString());
                            log.setFid(info.get("FID").toString());
                            log.setRid(info.get("RID").toString());
                            log.setRequesturl(info.get("REQUESTURL").toString());
                            log.setLogtype(info.get("LOGTYPE").toString());
                            log.setLoginip(info.get("LOGINIP").toString());
                            log.setOperatorid(info.get("OPERATORID").toString());
                            log.setOperatorcode(info.get("OPERATORCODE").toString());
                            log.setOperatorname(info.get("OPERATORNAME").toString());
                            log.setOperattime(info.get("OPERATTIME").toString());
                            log.setLogcontent(info.get("LOGCONTENT").toString());
                            log.setUsercustom1(info.get("PRONAME").toString());
                            log.setUsercustom2(flowname);
                            log.setUsercustom3(info.get("CARDNO").toString()+"\t");
                            log.setUsercustom4(info.get("NAME").toString());
                            log.setUsercustom5(info.get("JNAME").toString());

                            fillLogRsultAndRemark(flowname,log);
                            logList.add(log);
                        }
                    }
                    resultBean.setState(true);
                    resultBean.setMessage("操作成功");
                    resultBean.setTotalPage("1");
                    resultBean.setDatas(logList);
                    if (logs==null){
                        resultBean.setTotalResult(0+"");
                    }else{
                        resultBean.setTotalResult(logs.size()+"");
                    }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  resultBean;
    }

    private SystemLog fillLogRsultAndRemark(String flowName,SystemLog log){
        if (log.getRequesturl().indexOf("ChangeTelAction")>0) {
            //JSONObject jsonObject = JSONObject.fromObject(log.getLogcontent());
            //DUserResume resume = (DUserResume) JSONObject.toBean(jsonObject, DUserResume.class);
            log.setLogresult("更新手机号");
        }else  if (log.getRequesturl().indexOf("NextAction")>0) {
            JSONObject jsonObject = JSONObject.fromObject(log.getLogcontent());
            DUserResume resume = (DUserResume) JSONObject.toBean(jsonObject, DUserResume.class);
            log.setLogresult("流转至下一步");
        }else if (log.getRequesturl().indexOf("PublishResultAction")>0){
            JSONObject jsonObject = JSONObject.fromObject(log.getLogcontent());
            DUserResume resume = (DUserResume) JSONObject.toBean(jsonObject, DUserResume.class);
            log.setLogresult("发布操作");
        }else if (log.getRequesturl().indexOf("syncDuserResume")>0){
            JSONObject jsonObject = JSONObject.fromObject(log.getLogcontent());
            DUserResume resume = (DUserResume) JSONObject.toBean(jsonObject, DUserResume.class);
            log.setLogresult("简历同步");
        }else{
            if (flowName.equalsIgnoreCase("简历筛选")){
                JSONObject jsonObject=JSONObject.fromObject(log.getLogcontent());
                DUserResume resume=(DUserResume)JSONObject.toBean(jsonObject, DUserResume.class);
                log.setLogresult(resume.getState());
                log.setLogremark(resume.getSxbz());
            }else if (flowName.equalsIgnoreCase("笔试")){
                JSONObject jsonObject=JSONObject.fromObject(log.getLogcontent());
                DoBSUser resume=(DoBSUser)JSONObject.toBean(jsonObject, DoBSUser.class);
                log.setLogresult(resume.getResult());
                log.setLogremark(resume.getRemark());
            }else if (flowName.equalsIgnoreCase("面试")){
                JSONObject jsonObject=JSONObject.fromObject(log.getLogcontent());
                DoMSUser resume=(DoMSUser)JSONObject.toBean(jsonObject, DoMSUser.class);
                log.setLogresult(resume.getResult());
                log.setLogremark(resume.getRemark());
            }else if (flowName.equalsIgnoreCase("资料审核")){
                JSONObject jsonObject=JSONObject.fromObject(log.getLogcontent());
                DUserResume resume=(DUserResume)JSONObject.toBean(jsonObject, DUserResume.class);
                log.setLogresult(resume.getZlstate());
                log.setLogremark(resume.getZlremark());
            }else if (flowName.equalsIgnoreCase("笔试")){
                JSONObject jsonObject=JSONObject.fromObject(log.getLogcontent());
                DoBSUser resume=(DoBSUser)JSONObject.toBean(jsonObject, DoBSUser.class);
                log.setLogresult(resume.getResult());
                log.setLogremark(resume.getRemark());
            }else if (flowName.equalsIgnoreCase("政审")||flowName.equalsIgnoreCase("背景调查")){
                JSONObject jsonObject=JSONObject.fromObject(log.getLogcontent());
                DUserResume resume=(DUserResume)JSONObject.toBean(jsonObject, DUserResume.class);
                log.setLogresult(resume.getZsstate());
                log.setLogremark(resume.getZsremark());
            }else if (flowName.equalsIgnoreCase("实作")){
                JSONObject jsonObject=JSONObject.fromObject(log.getLogcontent());
                DoOperationUser resume=(DoOperationUser)JSONObject.toBean(jsonObject, DoOperationUser.class);
                log.setLogresult(resume.getResult());
                log.setLogremark(resume.getBz());
            }else if (flowName.equalsIgnoreCase("体检")){
                JSONObject jsonObject=JSONObject.fromObject(log.getLogcontent());
                DUserResume resume=(DUserResume)JSONObject.toBean(jsonObject, DUserResume.class);
                log.setLogresult(resume.getTjstate());
                log.setLogremark(resume.getTjremark());
            }else if (flowName.equalsIgnoreCase("签约")){
                JSONObject jsonObject=JSONObject.fromObject(log.getLogcontent());
                DUserResume resume=(DUserResume)JSONObject.toBean(jsonObject, DUserResume.class);
                log.setLogresult(resume.getQystate());
                log.setLogremark(resume.getQyremark());
            }else if (flowName.equalsIgnoreCase("实习")){
                JSONObject jsonObject=JSONObject.fromObject(log.getLogcontent());
                DUserResume resume=(DUserResume)JSONObject.toBean(jsonObject, DUserResume.class);
                log.setLogresult(resume.getSxstate());
                log.setLogremark(resume.getSxremark());
            }else if (flowName.equalsIgnoreCase("录用")){
                JSONObject jsonObject=JSONObject.fromObject(log.getLogcontent());
                DUserResume resume=(DUserResume)JSONObject.toBean(jsonObject, DUserResume.class);
                log.setLogresult(resume.getBdstate());
                log.setLogremark(resume.getBdremark());
            }
        }



        return  log;
    }

}
